package client;

import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
import com.jme3.scene.shape.Sphere;
import com.jme3.ui.Picture;
import com.jme3.util.BufferUtils;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.Console;
import de.lessvoid.nifty.controls.Label;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.tools.SizeValue;

/**
 *
 * @author Ben
 */


public class GameInterface
{
    NiftyJmeDisplay niftyD;
    Nifty nifty;
    Element niftyConsole;
    Console niftyConsoleC;
    Element consolePop;
    Picture ring = new Picture("HudSelect");
    InterfaceButton bar1 = new InterfaceButton();
    InterfaceButton bar2 = new InterfaceButton();
    InterfaceButton bar3 = new InterfaceButton();
    Picture hud = new Picture("HeadsUp");
    Box uiBox = new Box(Vector3f.ZERO, 15f, 15f, 15f);
    Sphere uiSphere = new Sphere(6, 6, 15);
    Cylinder uiCylinder = new Cylinder(6, 18, 5, 10, true);
    Geometry gBox = new Geometry("uiBox", uiBox);
    Geometry gSphere = new Geometry("uiSphere", uiSphere);
    Geometry gCylinder = new Geometry("uiCylinder", uiCylinder);
    Material noTex;
    BitmapFont guiFont;
    BitmapText fpsText;
    Element progressBar;
    Label loadtext;
    private TextRenderer textr;
    float frameCount = 0;
    boolean gameLoading = false;
    int barSize = 64;
    int maxbars = 3;
    int curbar = 1;
    int curbarLastPoll = 1;
    int choiceSizeLastPoll = 6;
    int[] barpoints =
    {
        0, 64, 64, 64
    };
    float screenhalf;
    
    class InterfaceButton
    {
        String tex;
        int size;
        int size2;
        float posx;
        float posy;
        InterfaceButton()
        {
            
        }
        
        public void createSquare(String ident,String tex, int size, float posx, float posy)
        {
            Picture temp = new Picture();
            Mesh tempm = new Mesh();
            Vector3f[] verts = new Vector3f[4];
            verts[0] = new Vector3f(0,0,0);
            verts[1] = new Vector3f(1,0,0);
            verts[2] = new Vector3f(0,1,0);
            verts[3] = new Vector3f(1,1,0);
            Vector2f[] tc = new Vector2f[4];
            tc[0] = new Vector2f(0,0);
            tc[1] = new Vector2f(1,0);
            tc[2] = new Vector2f(0,1);
            tc[3] = new Vector2f(1,1);
            int [] in = { 2,0,1 , 1,3,2};
            tempm.setBuffer(Type.Position,3,BufferUtils.createFloatBuffer(verts));
            tempm.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(tc));
            tempm.setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(in));
            tempm.updateBound();
            temp.setImage(Main.appref.getAssetManager(), tex, true);
            temp.setHeight(size);
            temp.setWidth(size);
            temp.setPosition(screenhalf+posx, posy);
            temp.setName(ident);
            temp.setMesh(tempm);
            Main.appref.getGuiNode().attachChild(temp);
        }
    }
    
    /*
    start_interface:
    initialises all basic interface parts (hud, bar etc) and connects them to the gui, also inits nifty
    may or may not need to be ported to nifty in a future rev
    */
    public void startInterface(SimpleApplication app)
    {
       app.getGuiNode().detachAllChildren();
       app.getStateManager().attach(new ViewPortState());
       //niftyD = new NiftyJmeDisplay(Main.api.getAssetManager(),Main.api.getInputManager(),Main.api.getAudioRenderer(),Main.api.getGuiViewPort());
       nifty  = niftyD.getNifty();
       screenhalf = Main.appref.getGuiViewPort().getCamera().getWidth()/2.5f;
       Main.appref.getGuiViewPort().addProcessor(niftyD);
       noTex = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
       noTex.setColor("Color", ColorRGBA.Blue);
       gBox.setMaterial(noTex);
       gSphere.setMaterial(noTex);
       gCylinder.setMaterial(noTex);
       gCylinder.rotate(0,90,0);
       guiFont = app.getAssetManager().loadFont("Interface/Fonts/Default.fnt");
       bar1.createSquare("bar1", "Textures/interface_sheet.png", barSize, 0, 0);
       bar2.createSquare("bar2", "Textures/interface_sheet.png", barSize, 64, 0);
       bar3.createSquare("bar1", "Textures/interface_sheet.png", barSize, 128, 0);
       ring.setImage(app.getAssetManager(), "Textures/interface_select.png", true);
       hud.setImage(app.getAssetManager(), "Textures/interface_hud.png", true);
       ring.setWidth(barSize);
       ring.setHeight(barSize);
       hud.setHeight(100f);
       hud.setWidth(400f);
       hud.setPosition(0, app.getGuiViewPort().getCamera().getHeight()-100f);
       ring.setPosition(screenhalf, 0);
       gBox.setLocalTranslation(screenhalf+barSize/2, barSize/2, 0);
       gSphere.setLocalTranslation(screenhalf+(barSize*6)/4,barSize/2,0);
       gCylinder.setLocalTranslation(screenhalf+(barSize*5)/2,barSize/2,0);
       fpsText = new BitmapText(guiFont,false);
       fpsText.setSize(guiFont.getCharSet().getRenderedSize());
       fpsText.setLocalTranslation(100,100,100);
       app.getGuiNode().attachChild(fpsText);
       app.getGuiNode().attachChild(hud);
       app.getGuiNode().attachChild(ring);         
       app.getGuiNode().attachChild(gBox);
       app.getGuiNode().attachChild(gSphere);
       app.getGuiNode().attachChild(gCylinder);
    }
    
    /*
    init_load:
    sets up the loading screen and adds it to the gui view
    */
    public void initLoadScreen()
    {
        Main.appref.getFlyByCamera().setEnabled(false);
        Main.appref.getGuiNode().detachAllChildren();
        niftyD = new NiftyJmeDisplay(Main.appref.getAssetManager(), 
                Main.appref.getInputManager(), 
                Main.appref.getAudioRenderer(), 
                Main.appref.getGuiViewPort());
        nifty = niftyD.getNifty();
        nifty.fromXml("Interface/loading-bar.xml", "start");
        progressBar = nifty.getScreen("loadlevel").findElementByName("progressbar");
        loadtext = nifty.getScreen("loadlevel").findNiftyControl("loadingtext", Label.class);
        Main.appref.getGuiViewPort().addProcessor(niftyD);
    }
    
    /*
    load_console:
    sets up the console
    */
    public void loadConsole()
    {
        nifty = niftyD.getNifty();
        nifty.fromXml("Interface/console.xml", "start");
        niftyConsole = nifty.createPopupWithId("console", "console");
        niftyConsoleC = niftyConsole.findNiftyControl("console", Console.class);
        niftyConsoleC.output("Lund Console Initialised");
        consolePop = niftyConsole.findElementByName("console");
    }
    
    /*
    finish_load:
    finishes loading and regrabs the cursor
    */
    public void finishLoad()
    {
        Main.appref.getFlyByCamera().setEnabled(true);
        Main.appref.getInputManager().setCursorVisible(false);
        gameLoading = false;
    }
    
    /*
    load_menu:
    shifts the program to the loadscreen
    */
    public void loadMenu()
    {
        nifty.gotoScreen("loadlevel");
        gameLoading = true;
    }
    
    /*
    load_progress:
    adjusts the size of the loading bar as per the progress state
    */
    public void setLoadProgress(final float progress, String text)
    {
        final int WIDTH = 32;
        int pixw = (int) (WIDTH + (progressBar.getParent().getWidth() - WIDTH * progress));
        progressBar.setConstraintWidth(new SizeValue(pixw + "px"));
        progressBar.getParent().layoutElements();
        loadtext.setText(text);
    }
    
    /*
    scroll_up/down:
    adjusts the curbar variables for the movement of the bar and moves the selection ring
    */
    public void scrollUp()
    {
        if (curbar < maxbars)
        {
            curbarLastPoll = curbar;
            curbar = curbar + 1;
            ring.move(barpoints[curbar], 0, 0);
        }
    }
    
    public void scrollDown()
    {
        if (curbar > 1 && curbar != maxbars+1)
        {
            curbarLastPoll = curbar;
            curbar = curbar - 1;
            ring.move(-barpoints[curbar], 0, 0);
        }
    }
    
    /*
    barChange:
    checks if the bar has changed, mainly to stop recursive loops
    */
    public boolean barChange()
    {
        if(curbar != curbarLastPoll || Main.player.choiceSize != choiceSizeLastPoll)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /*
    update_numbers:
    moves the interface numbers to their appropriate locations
    */
    public void updateNumbers()
    {
        float i = Math.round(Main.appref.getFPS());
        fpsText.setText("Lund - FPS: " + i);
    }
}
